<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="80%" :title="`${drawerProps.title}文章`">
    <el-form
      ref="roleFormRef"
      label-width="150px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="item"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="文章标题" prop="name">
        <el-input v-model="item.name" placeholder="请填写文章标题" clearable></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cateCode">
        <el-select v-model="item.cateCode" placeholder="请选择文章分类">
          <el-option v-for="cate in articleCateData" :key="cate.code" :label="cate.name" :value="cate.code"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="item.author" placeholder="请填写作者" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="item.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input v-model="item.sort" placeholder="请填写排序值" clearable></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="brief">
        <el-input v-model="item.brief" placeholder="请填写简介" clearable></el-input>
      </el-form-item>
      <el-form-item label="seoKeywords" prop="seoKeywords">
        <el-input v-model="item.seoKeywords" placeholder="请填写seoKeywords" clearable></el-input>
      </el-form-item>
      <el-form-item label="seoDescription" prop="seoDescription">
        <el-input v-model="item.seoDescription" placeholder="请填写seoDescription" clearable></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="imageShow">
        <UploadImg v-model:image-url="item.imageShow" width="200px" height="200px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传文章封面</span>
          </template>
          <template #tip> 图片大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <WangEditor v-model:value="item.content" height="400px" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="ArticleEdit">
import { ref, reactive, onUpdated } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Cms } from "@/api/interface";
import { getArticleInfo, getArticleCateList } from "@/api/modules/cms";
import WangEditor from "@/components/WangEditor/index.vue";
import UploadImg from "@/components/Upload/Img.vue";

let item = ref<Cms.ArticleInfo>({
  id: 0,
  cateCode: "",
  name: "",
  author: "",
  img: "",
  status: 1,
  sort: 0,
  brief: "",
  seoKeywords: "",
  seoDescription: "",
  content: "",
  imageShow: ""
});

let articleCateData = ref();

onUpdated(() => {
  if (drawerProps.value.title == "编辑") {
    const adminRole = getArticleInfo({ id: drawerProps.value.row.id });
    adminRole.then(res => {
      item.value = res.data;
    });
  } else {
    item.value = {
      id: 0,
      cateCode: "",
      name: "",
      author: "",
      img: "",
      status: 1,
      sort: 0,
      brief: "",
      seoKeywords: "",
      seoDescription: "",
      content: "",
      imageShow: ""
    };
  }

  const menuSelect = getArticleCateList({ page: 1, pageSize: 1000, name: "" });
  menuSelect.then(res => {
    articleCateData.value = res.data.rows;
  });
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Cms.ArticleList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};
const rules = reactive({
  name: [{ required: true, message: "请填写文章标题" }],
  cateCode: [{ required: true, message: "请选择文章分类" }],
  author: [{ required: true, message: "请填写作者" }],
  content: [{ required: true, message: "请填写内容" }]
});
// 提交数据（新增/编辑）
const roleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  if (item.value.imageShow.length > 0) {
    item.value.img = new URL(item.value.imageShow).pathname;
  }
  roleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(item.value);
      ElMessage.success({ message: `${drawerProps.value.title}文章成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
